package UI
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.filters.GlowFilter;	

	public class UICheck extends Sprite
	{
		public var mCaptionColor:int=0x000000;
		public var mCaption:TextField=null;
		public var mClickFunc:Function=null;
		public var mWidth:Number=0;
		public var mHeight:Number=0;
		public var mChecked:Boolean=false;
		
		public function UICheck()
		{
			super();
		}
		
		public function Create(x:int,y:int,w:int,h:int,caption:String,clickFunc:Function):Boolean
		{
			this.x=x;
			this.y=y;
			mWidth=w;
			mHeight=h;
			alpha=0.75;

			mCaption=new TextField;
			addChild(mCaption);
			mCaption.width=mWidth-mHeight-8;
			mCaption.height=mHeight;
			mCaption.defaultTextFormat=UIManager.msInstance.mUITextFormat;
			mCaption.textColor=mCaptionColor;
			mCaption.selectable=false;
			mCaption.cacheAsBitmap=true;
			//mCaption.filters = [new GlowFilter(0x000000,1,1.2,1.2,100)];
			SetCaption(caption);
			mClickFunc=clickFunc;
			
			
			addEventListener(MouseEvent.MOUSE_DOWN,OnMouseClick);
			addEventListener(MouseEvent.MOUSE_OVER,OnMouseMove);
			addEventListener(MouseEvent.MOUSE_OUT,OnMouseOut);
			
			mChecked=!mChecked;
			Flip();
			return true;
		}
		
		public function SetCaption(caption:String):void
		{
			mCaption.text=caption;
			mCaption.x= mHeight+8;
			mCaption.y=mHeight/2-mCaption.textHeight/2-2;
		}
		
		public function SetChecked(checked:Boolean):void
		{
			if(mChecked==checked)
				return;
			
			mChecked=checked;
			
			if(mChecked)
			{
				graphics.lineStyle(3,0x000000,0.75);
				graphics.moveTo(8,8);
				graphics.lineTo(mHeight-8,mHeight-8);
				graphics.moveTo(mHeight-8,8);
				graphics.lineTo(8,mHeight-8);
			}
			else
			{
				graphics.clear();
				graphics.lineStyle(2,0x000000,0.75);
				graphics.beginFill(0xFFFFFF,0.25);
				graphics.drawRoundRect(4,4,mHeight-8,mHeight-8,4,4);
				graphics.endFill();
			}
		}
		
		public function Flip():void
		{
			SetChecked(!mChecked);
		}
		
		public function OnMouseClick(event:MouseEvent):void
		{
			Flip();
			if(mClickFunc!=null)
				mClickFunc();
			
			event.stopImmediatePropagation();
		}
		
		public function OnMouseMove(event:MouseEvent):void
		{
			this.alpha=0.9;
		}
		public function OnMouseOut(event:MouseEvent):void
		{
			this.alpha=0.75;
		}
	}
}